
import ElementUI from "element-ui";

// =================================用于处理日期时间===========================================

/**
 * 得到年x月x日 时：分：秒的格式的时间表
 * 
 * @param {*} type 默认为0,获取整个日期时间, 1 只获取 年-月-日, 2 只获取 时：分：秒,
 * @param {*} separator 默认为'-'  分隔符
 * @param {*} 
 * 
 */
export const getDateTime = (type = 0, separator = '-') => {
	const date = new Date();
	const year = date.getFullYear() + separator;
	const month = (date.getMonth() + 1).toString().padStart(2, '0') + separator;
	const day = date.getDate().toString().padStart(2, '0');
	const hour = date.getHours().toString().padStart(2, '0') + ':';
	const minute = date.getMinutes().toString().padStart(2, '0') + ":";
	const second = date.getSeconds().toString().padStart(2, '0');

	if (type === 0) {
		const result = year + month + day + ' ' + hour + minute + second;
		return result;
	} else if (type == 1) {
		const result = year + month + day;
		return result;
	} else if (type == 2) {
		const result = hour + minute + second;
		return result;
	} else if (type == 3) {

	}
}
/**
 * 得到当日为星期几
 * 
 */

export const getWeeks = () => {
	const date = new Date();
	const num = date.getDay();
	const weekList = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日']
	return weekList[num - 1]
}

// =================================用于规则效验===========================================

/**
 * 身份证号码规则效验
 * @param {*} idCard 身份证
 * 
 */
export const idCardValidation = (idCard) => {


	if (!/(^\d{15}$)|(^\d{17}(\d|X|x)$)/.test(idCard)) {
		console.log('你输入的身份证长度或格式错误')
		ElementUI.Message.error("你输入的身份证长度或格式错误");
		return false
	}

	//身份证城市
	var aCity = {
		11: "北京",
		12: "天津",
		13: "河北",
		14: "山西",
		15: "内蒙古",
		21: "辽宁",
		22: "吉林",
		23: "黑龙江",
		31: "上海",
		32: "江苏",
		33: "浙江",
		34: "安徽",
		35: "福建",
		36: "江西",
		37: "山东",
		41: "河南",
		42: "湖北",
		43: "湖南",
		44: "广东",
		45: "广西",
		46: "海南",
		50: "重庆",
		51: "四川",
		52: "贵州",
		53: "云南",
		54: "西藏",
		61: "陕西",
		62: "甘肃",
		63: "青海",
		64: "宁夏",
		65: "新疆",
		71: "台湾",
		81: "香港",
		82: "澳门",
		91: "国外"
	};

	if (!aCity[parseInt(idCard.substr(0, 2))]) {
		console.log('你的身份证地区非法')
		ElementUI.Message.error("你的身份证地区非法");
		return false
	}

	// 出生日期验证
	var birthDate = (idCard.substr(6, 4) + "-" + Number(idCard.substr(10, 2)) + "-" + Number(idCard.substr(12, 2))).replace(/-/g, "/");

	let date = new Date(birthDate)

	if (birthDate != (date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate())) {
		console.log('身份证上的出生日期非法')
		ElementUI.Message.error("身份证上的出生日期非法");
		return false
	}

	// 身份证号码校验
	var sum = 0,
		weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2],
		codes = "10X98765432"
	for (var i = 0; i < idCard.length - 1; i++) {
		sum += idCard[i] * weights[i];
		console.log("sum-------", sum);
	}

	var last = codes[sum % 11]; //计算出来的最后一位身份证号码
	console.log("last-----", last);
	if (idCard[idCard.length - 1] != last) {
		console.log('你输入的身份证号非法')
		ElementUI.Message.error("你输入的身份证号非法");
		return false
	}
	console.log('身份证验证成功');


}

/**
 * 电话号码规则效验
 * @param {*} iphone 电话号码
 * 
 */

export const iphoneValidation = (iphone) => {
	const result = /^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\d{8}$/.test(iphone);
	console.log("result-------", result);
	if (!result) {
		ElementUI.Message.error('你的电话号码长度或格式错误')
	}

}

/**
 * 邮箱规则效验
 * @param {*} email 邮箱
 * 
 */
export const emailValidation = (email) => {
	const result = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/.test(s);
	console.log("result-------", result);
	if (!result) {
		ElementUI.Message.error('你的邮箱长度或格式错误')
	}

}


// =================================用于判断运行环境===========================================

/**
 * 是否是微信浏览器
 * 
 */
export const isWeiXin = () => {
	const ua = navigator.userAgent.toLowerCase();
	return ua.match(/microMessenger/i) == 'micromessenger'
}

/**
 * 是否是移动端
 * 
 */
export const isDeviceMobile = () => {
	const ua = navigator.userAgent.toLowerCase();
	return /android|webos|iphone|ipod|balckberry/i.test(ua)
}

/**
 * 是否是qq浏览器
 * 
 */
export const isQQBrowser = () => {
	const ua = navigator.userAgent.toLowerCase();
	return /mqqbrowser|qzone|qqbrowser|qbwebviewtype/i / test(ua)
}
/**
 * 是否是ios
 * 
 */
export const isIos = () => {
	const u = navigator.userAgent;
	if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) {  //安卓手机
		return false
	} else if (u.indexOf('iPhone') > -1) {//苹果手机
		return true
	} else if (u.indexOf('iPad') > -1) {//iPad
		return false
	} else if (u.indexOf('Windows Phone') > -1) {//winphone手机
		return false
	} else {
		return false
	}

}
/**
 * 是否为pc端
 * 
 */
export const isPC = () => {
	var userAgentInfo = navigator.userAgent;
	var Agents = ["Android", "iPhone",
		"SymbianOS", "Windows Phone",
		"iPad", "iPod"];
	var flag = true;
	for (var v = 0; v < Agents.length; v++) {
		if (userAgentInfo.indexOf(Agents[v]) > 0) {
			flag = false;
			break;
		}
	}
	return flag;
}

// =================================用于处理url参数===========================================

/**
 * 获取url参数 
 * @params url 
 * @params type 将url参数格式化成对象的形式, 默认为0, 不格式化, 1时进行对象格式化
 * 
 */
export const getUrlParams = (url, type = 0) => {
	const param = url.split('?')[1];
	if (type == 0) {
		return decodeURI(param);
	} else if (type == 1) {
		let arr = param.split('&');
		let obj = {};
		for (let i = 0; i < arr.length; i++) {
			obj[arr[i].split('=')[0]] = decodeURI(arr[i].split('=')[1])
		}
		return obj;

	}
}



// =================================用于加密处理===========================================
/**
 * 加密处理
 * 
 * 函数名 dataEncryption 数据加密
 * @params content 加密内容
 * @params type 加密的类型,name 名字  idCard 身份证号  tel 电话号码
 * @params authority 权限  默认为0为普通用户, 进行加密， 1的话为超级管理员 不进行加密处理
 */
export const dataEncryption = (content, type, authority = 0) => {
	if (!authority) {
		if (type == 'name') {
			const result = content.length > 2 ? content[0] + "*".repeat(content.length - 2) + content[content.length - 1] : name[0] + "*";
			return result
		} else if (type == "idCard") {
			const result = content.replace(/^(\d{6})\d{8}(\d{3}[0-9X])$/, '$1********$2');
			return result;
		} else if (type == "tel") {
			const str = content.toString();
			const result = str.replace(/(\d{3})\d{4}(\d{4})/, "$1****$2")
			return result;

		}
	}else{
		return content
	}



}

// =================================格式转换===========================================
 /**
    * 金额大写
    * @param price 要转换大写的金额
    */
 export const moneyBigWrite = (price)=>{
	let fraction = ['角', '分'];
        let digit = [
            '零', '壹', '贰', '叁', '肆',
            '伍', '陆', '柒', '捌', '玖'
        ];
        let unit = [
            ['元', '万', '亿'],
            ['', '拾', '佰', '仟']
        ];
        let head = price < 0 ? '欠' : '';
        price = Math.abs(price);
        let s = '';
        for (let i = 0; i < fraction.length; i++) {
            s += (digit[Math.floor(price * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '');
        }
        s = s || '整';
        price = Math.floor(price);
        for (let i = 0; i < unit[0].length && price > 0; i++) {
            let p = '';
            for (var j = 0; j < unit[1].length && price > 0; j++) {
                p = digit[price % 10] + unit[1][j] + p;
                price = Math.floor(price / 10);
            }
            s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s;
        }
        return head + s.replace(/(零.)*零元/, '元')
            .replace(/(零.)+/g, '零')
            .replace(/^整$/, '零元整');
}

/**
 * @param text  复制的文本
 * 
 */
export const copy = (text)=>{

	return navigator.clipboard && navigator.clipboard.writeText && navigator.clipboard.writeText(text)
}